package com.yunhai.support.service.impl;

import com.alibaba.fastjson.JSON;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dyvmsapi.model.v20170525.SingleCallByTtsRequest;
import com.aliyuncs.dyvmsapi.model.v20170525.SingleCallByTtsResponse;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.gson.Gson;
import com.yunhai.common.dto.GlobalException;
import com.yunhai.common.dto.PaginationVo;
import com.yunhai.support.entity.PhoneVoiceMessage;
import com.yunhai.support.entity.vo.VoiceMessageVo;
import com.yunhai.support.mapper.AliVoiceMessageMapper;
import com.yunhai.support.service.AliVoiceMessageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Slf4j
@Service
public  class  AliVoiceMessageServiceImpl implements AliVoiceMessageService {

    @Resource
    private AliVoiceMessageMapper aliVoiceMessageMapper;
    @Resource
    private IAcsClient iAcsClient;
    private String ttsCode = "TTS_180345498";
    private Integer playTimes = 2;
    private Integer speed = 0;

    @Transactional
    public void sendVoiceNotification(String phone,
                                      String serialNumber,
                                      String name,
                                      String strength,
                                      String energy,
                                      Integer balance,
                                      Integer repeatCount) throws Exception {

       Map map = new HashMap();
       map.put("name", name);
       map.put("balance", balance/100.0);
       map.put("strength", strength);
       map.put("energy", energy);
       map.put("imei", serialNumber);
       String jsonContent = JSON.toJSONString(map);

        SingleCallByTtsRequest request = new SingleCallByTtsRequest();
        request.setVersion("2017-05-25");
        request.setCalledNumber(phone);
        if( repeatCount == 0 ){
            request.setCalledShowNumber("057128356131");
            request.setTtsCode(ttsCode);
        } else {
            request.setCalledShowNumber("057128356124");
            request.setTtsCode(ttsCode);
        }
        request.setTtsParam(jsonContent);
        request.setPlayTimes(playTimes);
        request.setSpeed(speed);


        SingleCallByTtsResponse response = iAcsClient.getAcsResponse(request);
        String callId = response.getCallId();
        String code = response.getCode();
        boolean isSuccess = "OK".equals( code );
        PhoneVoiceMessage phoneVoiceMessage = new PhoneVoiceMessage();
        phoneVoiceMessage.setPhone(phone)
                .setSerialNumber(serialNumber)
                .setContent(jsonContent)
                .setDelFlag(0)
                .setSendStatus(0)
                .setCallId(callId)
                .setReason(response.getMessage());
        aliVoiceMessageMapper.insert(phoneVoiceMessage);
        System.out.println(new Gson().toJson(response));
        //电话拨打失败
        if( !isSuccess ){
            throw GlobalException.gen(10021, "电话拨打失败");
        }
    }


    @Override
    public void sendElectricVoiceNotification(String phone,
                                              String serialNumber,
                                              String name,
                                              Integer type,
                                              Integer repeatCount) throws Exception {
        Map map = new HashMap();
        map.put("name", name);
        map.put("imei", serialNumber);
        String jsonContent = JSON.toJSONString(map);

        SingleCallByTtsRequest request = new SingleCallByTtsRequest();
        request.setVersion("2017-05-25");
        request.setCalledNumber(phone);

        if( type == 1 ){
            //来电的话术
            request.setTtsCode("TTS_298670012");
        }else {
            //停电的话术
            request.setTtsCode("TTS_298735013");
        }

        if( repeatCount == 0 ){
            request.setCalledShowNumber("057128356131");
        } else {
            request.setCalledShowNumber("057128356124");
        }
        request.setTtsParam(jsonContent);
        request.setPlayTimes(playTimes);
        request.setSpeed(speed);

        SingleCallByTtsResponse response = iAcsClient.getAcsResponse(request);
        String callId = response.getCallId();
        String code = response.getCode();
        boolean isSuccess = "OK".equals( code );
        PhoneVoiceMessage phoneVoiceMessage = new PhoneVoiceMessage();
        phoneVoiceMessage.setPhone(phone)
                .setSerialNumber(serialNumber)
                .setContent(jsonContent)
                .setDelFlag(0)
                .setSendStatus(0)
                .setCallId(callId)
                .setReason(response.getMessage());
        aliVoiceMessageMapper.insert(phoneVoiceMessage);
        System.out.println(new Gson().toJson(response));
        //电话拨打失败
        if( !isSuccess ){
            throw GlobalException.gen(10021, "电话拨打失败");
        }
    }



    @Override
    public IPage getVoiceMessagePagination(PaginationVo paginationVo, VoiceMessageVo voiceMessageVo) throws Exception {

        LambdaQueryWrapper<PhoneVoiceMessage> lqs = new LambdaQueryWrapper<>();
        lqs.eq( voiceMessageVo.getSerialNumber()!= null, PhoneVoiceMessage::getSerialNumber, voiceMessageVo.getSerialNumber() );
        lqs.eq( voiceMessageVo.getPhoneNumber()!=null, PhoneVoiceMessage::getPhone, voiceMessageVo.getPhoneNumber() );
        lqs.ge(voiceMessageVo.getCreateStartTime() != null, PhoneVoiceMessage::getCreateTime, voiceMessageVo.getCreateStartTime() );
        lqs.le(voiceMessageVo.getCreateEndTime()!= null, PhoneVoiceMessage::getCreateTime, voiceMessageVo.getCreateEndTime());

        IPage page = new Page( paginationVo.getPageIndex(), paginationVo.getPageSize() );
        aliVoiceMessageMapper.selectPage( page, lqs );
        return page;
    }

    @Override
    public List<PhoneVoiceMessage> getVoiceMessageList() throws Exception {

        List<PhoneVoiceMessage> PhoneVoiceMessages = aliVoiceMessageMapper.selectList(null);
        return PhoneVoiceMessages;
    }


}

